package com.atguigu.campusintelligentassistantsystem.controller;

import com.atguigu.campusintelligentassistantsystem.entity.Canteen;
import com.atguigu.campusintelligentassistantsystem.entity.CanteenDish;
import com.atguigu.campusintelligentassistantsystem.entity.CanteenWindow;
import com.atguigu.campusintelligentassistantsystem.service.CanteenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/canteens")
@CrossOrigin(origins = "*")
public class CanteenController {

    @Autowired
    private CanteenService canteenService;

    /**
     * 获取所有食堂
     * @return 食堂列表
     */
    @GetMapping
    public ResponseEntity<List<Canteen>> getAllCanteens() {
        List<Canteen> canteens = canteenService.getAllCanteens();
        return ResponseEntity.ok(canteens);
    }

    /**
     * 根据ID获取食堂
     * @param id 食堂ID
     * @return 食堂信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<Canteen> getCanteenById(@PathVariable Long id) {
        Canteen canteen = canteenService.getCanteenById(id);
        if (canteen != null) {
            return ResponseEntity.ok(canteen);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 添加新食堂
     * @param canteen 食堂信息
     * @return 保存后的食堂
     */
    @PostMapping
    public ResponseEntity<Canteen> addCanteen(@RequestBody Canteen canteen) {
        Canteen savedCanteen = canteenService.addCanteen(canteen);
        return ResponseEntity.ok(savedCanteen);
    }

    /**
     * 更新食堂信息
     * @param id 食堂ID
     * @param canteen 食堂信息
     * @return 更新后的食堂
     */
    @PutMapping("/{id}")
    public ResponseEntity<Canteen> updateCanteen(@PathVariable Long id, @RequestBody Canteen canteen) {
        canteen.setId(id);
        Canteen updatedCanteen = canteenService.updateCanteen(canteen);
        return ResponseEntity.ok(updatedCanteen);
    }

    /**
     * 删除食堂
     * @param id 食堂ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteCanteen(@PathVariable Long id) {
        canteenService.deleteCanteen(id);
        return ResponseEntity.ok("食堂删除成功");
    }

    /**
     * 根据食堂ID获取窗口列表
     * @param canteenId 食堂ID
     * @return 窗口列表
     */
    @GetMapping("/{canteenId}/windows")
    public ResponseEntity<List<CanteenWindow>> getWindowsByCanteenId(@PathVariable Long canteenId) {
        List<CanteenWindow> windows = canteenService.getWindowsByCanteenId(canteenId);
        return ResponseEntity.ok(windows);
    }

    /**
     * 根据ID获取窗口
     * @param id 窗口ID
     * @return 窗口信息
     */
    @GetMapping("/windows/{id}")
    public ResponseEntity<CanteenWindow> getWindowById(@PathVariable Long id) {
        CanteenWindow window = canteenService.getWindowById(id);
        if (window != null) {
            return ResponseEntity.ok(window);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 添加新窗口
     * @param window 窗口信息
     * @return 保存后的窗口
     */
    @PostMapping("/windows")
    public ResponseEntity<CanteenWindow> addWindow(@RequestBody CanteenWindow window) {
        CanteenWindow savedWindow = canteenService.addWindow(window);
        return ResponseEntity.ok(savedWindow);
    }

    /**
     * 更新窗口信息
     * @param id 窗口ID
     * @param window 窗口信息
     * @return 更新后的窗口
     */
    @PutMapping("/windows/{id}")
    public ResponseEntity<CanteenWindow> updateWindow(@PathVariable Long id, @RequestBody CanteenWindow window) {
        window.setId(id);
        CanteenWindow updatedWindow = canteenService.updateWindow(window);
        return ResponseEntity.ok(updatedWindow);
    }

    /**
     * 删除窗口
     * @param id 窗口ID
     * @return 操作结果
     */
    @DeleteMapping("/windows/{id}")
    public ResponseEntity<String> deleteWindow(@PathVariable Long id) {
        canteenService.deleteWindow(id);
        return ResponseEntity.ok("窗口删除成功");
    }

    /**
     * 根据窗口ID获取菜品列表
     * @param windowId 窗口ID
     * @return 菜品列表
     */
    @GetMapping("/windows/{windowId}/dishes")
    public ResponseEntity<List<CanteenDish>> getDishesByWindowId(@PathVariable Long windowId) {
        List<CanteenDish> dishes = canteenService.getDishesByWindowId(windowId);
        return ResponseEntity.ok(dishes);
    }

    /**
     * 根据ID获取菜品
     * @param id 菜品ID
     * @return 菜品信息
     */
    @GetMapping("/dishes/{id}")
    public ResponseEntity<CanteenDish> getDishById(@PathVariable Long id) {
        CanteenDish dish = canteenService.getDishById(id);
        if (dish != null) {
            return ResponseEntity.ok(dish);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 添加新菜品
     * @param dish 菜品信息
     * @return 保存后的菜品
     */
    @PostMapping("/dishes")
    public ResponseEntity<CanteenDish> addDish(@RequestBody CanteenDish dish) {
        CanteenDish savedDish = canteenService.addDish(dish);
        return ResponseEntity.ok(savedDish);
    }

    /**
     * 更新菜品信息
     * @param id 菜品ID
     * @param dish 菜品信息
     * @return 更新后的菜品
     */
    @PutMapping("/dishes/{id}")
    public ResponseEntity<CanteenDish> updateDish(@PathVariable Long id, @RequestBody CanteenDish dish) {
        dish.setId(id);
        CanteenDish updatedDish = canteenService.updateDish(dish);
        return ResponseEntity.ok(updatedDish);
    }

    /**
     * 删除菜品
     * @param id 菜品ID
     * @return 操作结果
     */
    @DeleteMapping("/dishes/{id}")
    public ResponseEntity<String> deleteDish(@PathVariable Long id) {
        canteenService.deleteDish(id);
        return ResponseEntity.ok("菜品删除成功");
    }
}